﻿using Entity.ResultModel;
using Hotel.Models;
using IHotelBLL.IEmployeeBLL;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace Hotel.Controllers.Role
{
    public class RoleController : Controller
    {
        ResultModel result = new ResultModel();
        IRoleBLL _roleBLL;
        public RoleController(IRoleBLL roleBLL)
        {
            _roleBLL = roleBLL;
        }
        /// <summary>
        /// 获取展示的角色
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public IActionResult GetRoleInfos(int page,int limit)
        {
            var res = new
            {
                data = _roleBLL.GetRoles(page, limit, out int count),
                code = 0,
                count
            };
            return Json(res);
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public IActionResult AddRole(string roleName)
        {
            if(roleName is null)
            {
                result.Msg = "请输入角色名";
                return Json(result);
            }
            result.IsSuccess = _roleBLL.AddRole(roleName, out string msg);
            result.Msg = msg;
            return Json(result);
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IActionResult DeleteRole(string Id)
        {
            if(Id is null)
            {
                result.Msg = "未找到角色";
                return Json(result);
            }
            result.IsSuccess = _roleBLL.DeleteRole(Id, out string msg);
            result.Msg = msg;
            return Json(result);
        }
        /// <summary>
        /// 根据id获取角色信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IActionResult GetRoleInfo(string Id)
        {
            if(Id is null)
            {
                result.Msg = "未找到角色";
                return Json(result);
            }
            result.Data = _roleBLL.GetRole(Id);
            return Json(result);
        }
        public IActionResult GetEmployee(string roleId)
        {
            //获取穿梭框数据
            List<TransferOptionModel> options = _roleBLL.GetTransferOptions();
            //获取角色已经绑定的用户id集合
            List<string> bindEmployeeIds = _roleBLL.GetEmployee(roleId);

            result.IsSuccess = true;
            result.Status = 1;
            result.Data = new
            {
                Options = options,
                BindUserIds = bindEmployeeIds
            };
            return Json(result);
        }
        public IActionResult GetMenu(string roleId)
        {
            //获取穿梭框数据
            List<TransferOptionModel> options = _roleBLL.GetMenuTransferOptions();

            //获取角色已经绑定的用户id集合
            List<string> bindMenuIds = _roleBLL.GetBindMenuIds(roleId);

            result.IsSuccess = true;
            result.Status = 1;
            result.Data = new
            {
                Options = options,
                BindMenuIds = bindMenuIds
            };
            return Json(result);
        }

        /// <summary>
        /// 获取角色
        /// </summary>
        /// <returns></returns>
        public IActionResult GetRoles()
        {
            result.Data = _roleBLL.GetRoles();
            return Json(result);
        }
        /// <summary>
        /// 更新角色
        /// </summary>
        /// <param name="id"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public IActionResult UpdateRole(string id,string roleName)
        {
            if(id is null)
            {
                result.Msg = "未找到角色";
                return Json(result);
            }
            result.IsSuccess= _roleBLL.UpdateRole(id,roleName, out string msg);
            result.Msg = msg;
            return Json(result);
        }
        /// <summary>
        /// 员工绑定角色
        /// </summary>
        /// <returns></returns>
        public IActionResult BindEmployee(List<string> employeeIds, string roleId)
        {
            if (string.IsNullOrEmpty(roleId))
            {
                result.Msg = "未获取到角色id";
                return Json(result);
            }
            _roleBLL.BindEmployee(employeeIds, roleId);
            result.IsSuccess = true;
            result.Status = 1;
            result.Msg = "绑定成功";
            return Json(result);
        }
        public IActionResult BindMenu(List<string> menuIds, string roleId)
        {

            if (string.IsNullOrEmpty(roleId))
            {
                result.Msg = "未获取到角色id";
                return Json(result);
            }

            if (menuIds.Count < 1)
            {
                result.Msg = "未获取到用户id";
                return Json(result);
            }

            _roleBLL.BindMenu(menuIds, roleId);
            result.IsSuccess = true;
            result.Status = 1;
            result.Msg = "绑定成功";
            return Json(result);
        }
    }
}
